package StudyCode.summary.myfile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class SavePropertiesFile {
	public static void main(String[] args) {
		Properties table = new Properties();

		table.setProperty("color", "blue");
		table.setProperty("width", "200");

		System.out.println("设置 properties 后");
		listProperties(table);

		table.setProperty("color", "red");

		System.out.println("替换 properties 后");
		listProperties(table);

		saveProperties(table);
	}

	private static void saveProperties(Properties props) {
		try {
			FileOutputStream output = new FileOutputStream("props.dat");
			props.store(output, "Sample Properties");
			output.close();
			System.out.println("After saving properties");
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}

	private static void listProperties(Properties props) {
		Set<Object> keys = props.keySet();

		for (Object key : keys)
			System.out.printf("%s\t%s%n", key, props.getProperty((String) key));
		System.out.println();
	}
}
